package me.pinion.service;

import me.pinion.model.ModelType;
import me.pinion.model.Tag;
import me.pinion.model.XModel;

import java.util.List;

public class TagService extends Service{
    public List<Tag> search(String keyword){
        return search(keyword, 10);
    }

    public List<Tag> search(String keyword, int max){
        return Tag.dao.find("SELECT * FROM tag WHERE LOWER(name) LIKE ? LIMIT 0, ?", "%" + keyword.toLowerCase() + "%", max);
    }

    public List<Tag> popularTags(Class<? extends XModel<?>> clazz){
        List<Tag> tags = Tag.dao.paginate(1, 20, "SELECT t.*, count(tt.id) AS 'count' ",
                "FROM tag AS t, tag_object AS tt " +
                        "WHERE t.id=tt.tag_id AND tt.target_type_id=? " +
                        "GROUP BY t.id " +
                        "ORDER BY count(tt.id) DESC", ModelType.dao.getByClass(clazz).getId()).getList();
        return tags;
    }
}
